$(document).ready(function() {
	$('a.popup-trigger').click(function() {
		var popID = $(this).attr('rel'); // find corresponding pop-up

		// pop-up apparition
		$('#' + popID).fadeIn();

		//Effet fade-in du fond opaque
		$('body').append('<div id="fade"></div>'); //Ajout du fond opaque noir
		//Apparition du fond - .css({'filter' : 'alpha(opacity=80)'}) pour corriger les bogues de IE
		$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn();

		return false;
	});

	//Fermeture de la pop-up et du fond
	$('a.close, #fade').live('click', function() { //Au clic sur le bouton ou sur le calque...
		$('#fade , .popup-block').fadeOut(function() {
			$('#fade, a.close').remove();  //...ils disparaissent ensemble
		});
		return false;
	});

	// Nb posts slider
	$("#nbposts-slider")[0].onchange = function(e) {
		window.nbPosts = $("#nbposts-slider")[0].value;
		checkFb();
	}

	// Refresh slider
	$("#refresh-slider")[0].onchange = function(e) {
		window.refreshTime = $("#refresh-slider")[0].value;

		// disable Facebook fetching and relaunch it with the new interval
		clearInterval(window.fbCheckInterval);
		if (window.enableFBChecking==1) {
			checkFbEvery(window.refreshTime);
		}
	}

	// click on the refresh icon
	$("a.refresh-trigger").click(function() {
		//$("a.refresh-trigger img").css({'-webkit-transform': 'rotate(-360deg)'});
		//$("a.refresh-trigger img").css({'transform': 'rotate(-360deg)'});
		checkFb();
	});

	// Links icon
	$("#links-settings").click(function() {
		if ($("#links-settings img").attr('src') == "img/link-off.gif") {
			$("#links-settings img").attr('src', "img/link-on.gif");
			window.showLinks = 1;
		}
		else {
			$("#links-settings img").attr('src', "img/link-off.gif");
			window.showLinks = 0;
		}
	});

	// Sound icon
	$("#sound-settings").click(function() {
		if ($("#sound-settings img").attr('src') == "img/sound-off.gif") {
			$("#sound-settings img").attr('src', "img/sound-on.gif");
			window.enableSound = 1;
		}
		else {
			$("#sound-settings img").attr('src', "img/sound-off.gif");
			window.enableSound = 0;
		}
	});

	settingsOpened=0;
	// click on the settings icon
	$("a.settings-trigger").click(function() {
		if (settingsOpened==0) {
			// show settings menu
			$("#settings-panel").css({'visibility': 'visible'});
			$("#settings-panel").css({'right': '60px'});
			$("#settings-panel").css({'opacity': '1'});
			// rotate settings icon
			$("a.settings-trigger img").css({'-webkit-transform': 'rotate(-360deg)', 'transform': 'rotate(-360deg)'});
			$("a.settings-trigger img").css({'transform': 'rotate(-360deg)'});
			settingsOpened=1;
		}
		else {
			$("#settings-panel").css({'visibility': 'hidden'});
			$("#settings-panel").css({'right': '-60px'});
			$("#settings-panel").css({'opacity': '0'});
			$("a.settings-trigger img").css({'-webkit-transform': '', 'transform': ''});
			settingsOpened=0;
		}
	});

	addFormOpened=0;
	// click on the add icon
	$("a.add-trigger").click(function() {
		if (addFormOpened==0) {
			$("#add-panel input").css({'visibility': 'visible'});
			$("#add-panel input").css({'margin-left': '30px'});
			$("#add-panel input").css({'opacity': '1'});
			$("a.add-trigger img").css({'-webkit-transform': 'rotate(360deg)', 'transform': 'rotate(-360deg)'});
			$("a.add-trigger img").css({'transform': 'rotate(360deg)'});
			addFormOpened=1;
		}
		else {
			$("#add-panel input").css({'visibility': ''});
			$("#add-panel input").css({'margin-left': ''});
			$("#add-panel input").css({'opacity': ''});
			$("a.add-trigger img").css({'-webkit-transform': '', 'transform': ''});
			addFormOpened=0;
		}
	});

	// add post to Facebook stream
	$("#add-panel input").keypress(function(e) {
		if (e.keyCode==13 && $("#add-panel input").val() != '') {
			FB.api('/me/feed', 'post', { message: $("#add-panel input").val() }, function(response) {
			  if (!response || response.error) {
			    console.log('Error occured');
			  } else {
			    checkFb();
			  }
			});
			$("#add-panel input").val('');
		}
	});
});
